﻿package makeeasier.air.utils {
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	
	public class SettingsManager {
		
		private static var _settings:XML;
		private static var _settingsFile:File;
		private static var _settingsFileStream:FileStream;
        
		
		public function SettingsManager(){
			//Constructor
			
		}
		
		/*
		 * returns all settings as XML
		*/
		public static function getSettings():XML{
			setSettings();
			return _settings;
		}
		
		/*
		 * returns specified setting if 's' exists
		*/
		public static function getSet(s:String):String{
			setSettings();
			return query(s);
		}
		
		private static function setSettings(){
			_settingsFile = new File(File.applicationDirectory.nativePath + File.separator + "settings.xml");
			if(_settingsFile.exists){
				_settingsFileStream = new FileStream();
				_settingsFileStream.open(_settingsFile, FileMode.READ);
				_settings = new XML(_settingsFileStream.readUTFBytes(_settingsFileStream.bytesAvailable));
				_settingsFileStream.close();
			} else {
				throw new Error("Settings file couldn't read.");
			}
		}
		
		private static function query(s:String):String{
			var x:XMLList;
			return (_settings.setting.(@field == s)).text();
		}
	}
}